
<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
    <style>
        * {margin:0; padding: 0;}
        ul {
            list-style-type: none;
        }
        .box {
            width: 400px;
            height: 300px;
            border: 1px solid #ccc;
            margin: 100px auto;
            overflow: hidden;
        }
        .hd {
            height: 45px;
        }
        .hd span {
            display:inline-block;
            width: 90px;
            background-color: pink;
            line-height: 45px;
            text-align: center;
            cursor: pointer;
        }
        .hd span.current {
            background-color: purple;
        }
        .bd div {
            height: 255px;
            background-color: purple;
            display: none;
        }
        .bd div.current {
            display: block;
        }
    </style>

</head>
<body>
    <div class="box">
        <div class="hd" id="hd">
            <span class="current">体育</span>
            <span>娱乐</span>
            <span>新闻</span>
            <span>综合</span>
        </div>
        <div class="bd" id="bd">
            <div class="current">我是体育模块</div>
            <div>我是娱乐模块</div>
            <div>我是新闻模块</div>
            <div>我是综合模块</div>
        </div>
    </div>
   <!-- <script src="common.js"></script>
    <script>
        // 1 鼠标放到tab栏高亮显示，其它tab栏取消高亮
        var hd = my$('hd');
        var spans = hd.getElementsByTagName('span');
        for (var i = 0; i < spans.length; i++) {
            var span = spans[i];
            // 注册事件
            span.onmouseover = fn;
        }

        // 鼠标经过的事件处理函数
        function fn() {
            // 让所有的span取消高亮显示
            for (var i = 0; i < spans.length; i++) {
                var span = spans[i];
                span.className = '';
            }
            // 当前的span高亮显示
            this.className = 'current';
        }
        // var fn = function () {
                
        // }   
       // 2 tab栏对应的div 显示，其它div隐藏
        
    </script>-->
    <script>
        //获取span
        var hd=document.getElementById("hd");
        var spans=hd.getElementsByTagName("span");//伪数组  需要遍历
        //获取div
        var bd=document.getElementById("bd");
        var divs=bd.getElementsByTagName("div");//伪数组  需要遍历

        for (var i=0;i<spans.length;i++){
            var span=spans[i];//每一项 冰棍儿
            //span.index=i;//给每个span对象绑定一个index自定义属性或者用。
            span.setAttribute('index',i);
            span.onmouseover=function(){
                //排他 给span加背景色
                //清除span所有人和div所有人  写在一起
                for(var i=0;i<spans.length;i++){
                    var span=spans[i];
                    var div=divs[i];
                    span.className="";
                    div.className="";
                    //span.removeAttribute("class");
                    //div.removeAttribute("class");
                }
                //留下我自己
                //this.className="current";
                this.setAttribute("class","current");
                //获取当前对象的index值
                //var index=this.index;
                var index=this.getAttribute("index");
               // divs[index].className="current";
                divs[index].setAttribute("class","current");
            }
        }
    </script>
</body>
</html>
